package com.zqh.myfmmall.controller;



import com.zqh.myfmmall.rabbitmq.RabbitMqPublisher;
import com.zqh.myfmmall.result.Result;
import com.zqh.myfmmall.service.OrdersService;
import com.zqh.myfmmall.WxPay.WxPayService;
import com.zqh.myfmmall.utils.JwtUtil;
import com.zqh.myfmmall.vo.OrdersVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 订单 (Orders)表控制层
 *
 * @author KONO 曾淇杭哒！
 * @since 2022-01-20 20:53:00
 */
@RestController
@RequestMapping("orders")
@Api(tags = "订单管理")
@Slf4j
public class OrdersController {
    @Autowired
    private HttpServletRequest request;
    @Resource
    private OrdersService ordersService;
    @Resource
    private WxPayService wxPayService;
    @Resource
    private RabbitMqPublisher rabbitMqPublisher;

   @PostMapping("add")
    public Result add(@RequestParam("shoppingCartIds") List<String> shoppingCartIds,@RequestParam("addrId") String addrId){
       String token = request.getHeader("token");
       String userId = JwtUtil.getClaim(token).get("userId", String.class);
       try {
           Map<String, String> orderInfoMap = ordersService.addOrder(shoppingCartIds, addrId, userId);
           if (orderInfoMap != null) {
               String payUrl = wxPayService.wxPay(orderInfoMap);
               orderInfoMap.put("payUrl",payUrl);
               //发送mq延时信息，处理超时未支付
               rabbitMqPublisher.publishDelayedMessage("order:"+orderInfoMap.get("orderId"));
               return Result.success(orderInfoMap);
           }
           log.info("userId:{}库存不足生成订单失败",userId);
           return Result.fail("生成订单失败");
       } catch (SQLException e){
           log.warn("生成订单，数据库事务异常");
           return Result.fail("生成订单失败！");
       }catch (Exception e){
           log.warn("微信支付异常，订单生成失败");
           return Result.fail("生成订单失败！");
       }
   }

   @GetMapping("list")
    public Result list(@RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNum") Integer pageNum,@RequestParam("orderStatus") Integer status){
       String token = request.getHeader("token");
       String userId = JwtUtil.getClaim(token).get("userId", String.class);
       List<OrdersVO> orders = ordersService.pageByUserIdAndStatus(userId,status,pageSize,pageNum);
       return Result.success(orders);
   }

    /**
     * 功能描述:
     * 查询用于各状态订单的数量
     * @return
     * @author kono曾淇杭哒！
     * @date 2022/1/29 2:06
     */
   @GetMapping("counts")
    public Result orderCuonts(){
       String token = request.getHeader("token");
       String userId = JwtUtil.getClaim(token).get("userId", String.class);
       Map<String, Integer> countsMap = ordersService.orderCounts(userId);
       return Result.success(countsMap);
   }
}

